package com.hfdg.prototype.base.utils;


import com.hfdg.prototype.base.annotations.Column;
import com.hfdg.prototype.base.annotations.Table;
import org.apache.commons.lang3.reflect.FieldUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;
import java.lang.reflect.Field;
import java.sql.JDBCType;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class SqlBuilder {
	private static Logger logger = LoggerFactory.getLogger(SqlBuilder.class);
	private static List<String> getClassName(String filePath, List<String> className) {
		List<String> myClassName = new ArrayList<String>();
		File file = new File(filePath);
		File[] childFiles = file.listFiles();
		for (File childFile : childFiles) {
			if (childFile.isDirectory()) {
				myClassName.addAll(getClassName(childFile.getPath(), myClassName));
			} else {
				String childFilePath = childFile.getPath();
				childFilePath = childFilePath.substring(childFilePath.indexOf("\\classes") + 9,
						childFilePath.lastIndexOf("."));
				childFilePath = childFilePath.replace("\\", ".");
				myClassName.add(childFilePath);
			}
		}
		return myClassName;
	}
	public static List<String> getClassName(String packageName) {
		String filePath = ClassLoader.getSystemResource("").getPath() + packageName.replace(".", "\\");
		List<String> fileNames = getClassName(filePath, null);
		return fileNames;
	}
	public static  <T>String tableInit(Class<T> clazz){
		{
			String tableName = clazz.getAnnotation(Table.class).name();
			if(null==tableName){
				tableName=CamelUtils.camel2Underline(clazz.getSimpleName());
			}
			Field[] cfields = FieldUtils.getAllFields(clazz);
			List<Field> fields = new ArrayList<Field>(Arrays.asList(cfields));
			String dropSql = "drop table if exists "+tableName+";";
			String basicSql="create table "+tableName;
			String fieldsSql="(";
			for(Field f:fields){
				String fieldName = f.getName();
				Column column = f.getAnnotation(Column.class);
				String columnName = CamelUtils.camel2Underline(fieldName).toLowerCase();

				if(fieldName.equals("id")){
					fieldsSql+=columnName+" bigint  not null  auto_increment comment '主键id',";
				}else{
					JDBCType fieldType=column.type();
					if(JDBCType.BIGINT.equals(fieldType)){
						fieldsSql+=columnName+" decimal(20) default 0,";
					}else if(JDBCType.INTEGER==fieldType){
						fieldsSql+=columnName+" int(20) default 0,";
					}else if(JDBCType.VARCHAR==fieldType){
						if(null==column){
							fieldsSql+=columnName+" varchar(255) NULL DEFAULT NULL COMMENT '"+column.comment()+"' ,";
						}else{
							fieldsSql+=columnName+" "+column.type()+"("+column.length()+") NULL DEFAULT NULL COMMENT '"+column.comment()+"' ,";
						}
					}else if(JDBCType.TIMESTAMP==fieldType){
						fieldsSql+=columnName+" datetime(6) NULL DEFAULT NULL COMMENT '"+column.comment()+"' ,";
					}else if(JDBCType.CHAR ==fieldType){
						fieldsSql+=columnName+" char(4) NULL DEFAULT NULL COMMENT '"+column.comment()+"' ,";
					}else if(JDBCType.LONGVARCHAR .equals(fieldType)){
						fieldsSql+=columnName+" longtext NULL DEFAULT NULL COMMENT '"+column.comment()+"' ,";
					}
				}
			}
			fieldsSql+="primary key (id)";
			fieldsSql+=") AUTO_INCREMENT = 1 CHARACTER SET = utf8;";
			return (dropSql+basicSql+fieldsSql).toLowerCase();
		}
	}
}
